package com.qfedu.sign.common.untils;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    /**
     * 比较是不是今天
     * @param date
     * @return
     */
    public static boolean checkDay(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date).equals(sdf.format(new Date()));
    }
    /**
     * 比较2个日期的相差天数
     */
    public static int diffDays(Date sdate,Date edate){
        //需要考虑润年
        //实例化日历
        Calendar cal1=Calendar.getInstance();
        Calendar cal2=Calendar.getInstance();
        cal1.setTime(sdate);
        cal2.setTime(edate);
        //1.先验证年
        int yea1=cal1.get(Calendar.YEAR);
        int yea2=cal2.get(Calendar.YEAR);
        //2.记录天数
        int days1=cal1.get(Calendar.DAY_OF_YEAR);
        int days2=cal2.get(Calendar.DAY_OF_YEAR);
        //2020 2022
        //年需要比较
        if(yea1!=yea2){
            //跨年
            for(int i=yea1;i<yea2;i++){
                days2+=365;
                //四年一润，百年不润，四百年在润
                if((i%4==0 && i%100!=0) || i%400==0){
                    days2+=1;
                }
            }
        }
        return days2-days1;

    }
}
